//package com.tan.lighting.init;
//
//import com.tan.lighting.disruptor.handler.ProcessCoreCommandEventHandler;
//import com.tan.lighting.disruptor.snapshot.Snapshot;
//import com.tan.lighting.disruptor.snapshot.utils.SnapshotFileUtil;
//import com.tan.lighting.disruptor.snapshot.utils.SnapshotJsonUtil;
//import com.tan.framework.ddd.Command;
//import com.tan.framework.ddd.CommonCommand;
//import com.tan.framework.ddd.SingletonEntityRepository;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//
//import java.io.IOException;
//import java.lang.reflect.InvocationTargetException;
//import java.util.List;
//
///**
// * Created by tan on 2016/8/29.
// */
//
//public class InitProcessor {
//    @Autowired
//    private ProcessCoreCommandEventHandler coreCommandEventHandler;
//
//    @Autowired
//    private SingletonEntityRepository singletonEntityRepository;
//
//
//    @Autowired
//    private ApplicationContext applicationContext;
//
//    public void init() {
//        try {
//            recover();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//            System.exit(0);
//        }
//    }
//
//    private void recover() throws Throwable {
//        // snapshot 恢复
//        // core  snapshot恢复
//        Snapshot coreSnapshot = null;
//        try {
//            coreSnapshot = (Snapshot) SnapshotJsonUtil.recovery(coreCommandEventHandler.getFileBasePath(),
//                    Snapshot.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if (coreSnapshot != null) {
//            singletonEntityRepository.fill(
//                    (SingletonEntityRepository) coreSnapshot.getContentMap().get(SingletonEntityRepository.class));
//            //TODO
//
//        }
//
//        // core 命令
//
//        List<Command> commands = SnapshotFileUtil.read("./", coreCommandEventHandler.getjFileNamePrefix());
//        invokeCommands(commands);
//    }
//
//
//    private void invokeCommands(List<Command> commands)
//            throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
//        for (Command command : commands) {
//            if (command instanceof CommonCommand) {
//                CommonCommand cmd = (CommonCommand) command;
//                // List<Object> objects =
//                // applicationContext.getBeansOfType(Class.forName(cmd.getType()));
//                Class clazz = Class.forName(cmd.getType());
//                Object service = applicationContext.getBean(clazz);
//                if (cmd.getParameters() != null && cmd.getParameters().length > 0) {
//                    try {
//                        service.getClass().getMethod(cmd.getMethod(), cmd.getParameterTypes()).invoke(service,
//                                cmd.getParameters());
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                } else {
//                    service.getClass().getMethod(cmd.getMethod()).invoke(service);
//                }
//            }
//        }
//    }
//
////    private void initAdmin() {
////        String adminName = "admin";
////        String adminPwd = "123456";
////        int count = adminService.getAdminCount();
////        if (count == 0) {
////            adminService.addAdmin(adminName, adminPwd);
////        }
////    }
//}
